<?php include dirname(__FILE__).'/common/header.php'; ?>

<?php include dirname(__FILE__).'/common/menu.php'; ?>

var attr = {};
attr.ctrlName = '<?php echo $ctrlName; ?>';
attr.cardlist = <?php echo $cardlist ?>;

/*
| -------------------------------------------------------------------
|  List Grid
| -------------------------------------------------------------------
*/
list.grid = new ZM.GridPanel({
    url: list.baseUrl + attr.ctrlName+'/getCardTimeList',
    tbar:[{
        text:'添加',
        cls:'btn-show',
        handler:function() {
            var win = new ZM.PopUp({
                title:'添加时间设置',
                defaults:{labelWidth: 120},
                items:{
                    xtype:'formpanel',
                    url:list.baseUrl+ attr.ctrlName+'/createCardTime',
                    success:function(res) {
                        ZM.bo.msg('success',res.detail);
                        list.grid.store.reload();
                        win.close();
                    },
                    items:[{
                        xtype:'datefield',
                        fieldLabel:'日期',
                        format:'Y-m-d',
                        name:'card_time_date',
                        allowBlank:false
                    },{
                        xtype:'hidden',
                        fieldLabel:'开始时间',
                        name:'card_time_start',
                        value:'00:00:00',
                        format:'H:i:s',
                        allowBlank:false
                    },{
                        xtype:'hidden',
                        fieldLabel:'结束时间',
                        value:'23:59:59',
                        name:'card_time_end',
                        format:'H:i:s',
                        allowBlank:false
                    }].concat((function() {
                    	var arr = [];
                    	Ext.each(attr.cardlist,function(item,i) {
                            if(i >= 2) return;
							arr.push({
								xtype:'numberfield',
		                        fieldLabel:item[1]['CardName'] + '概率',
		                        name:'card_time_gift'+item[0]+'_rate',
		                        minValue:0,
		                        maxValue:1,
		                        allowBlank:false
							});
							arr.push({
								xtype:'intfield',
		                        fieldLabel:item[1]['CardName'] + '数量',
		                        minValue:0,
		                        name:'card_time_gift'+item[0]+'_count',
		                        allowBlank:false
							});
                    	});
                    	return arr;
                    })())
                }
            }).show();
        }
    }],
    columns:[
        {header:'ID',dataIndex:'card_time_id',hidden:true},
        {header:'日期',dataIndex:'card_time_date',sort:true,filter:{xtype:'daterangefield'}},
        {header:'开始时间',dataIndex:'card_time_start',sort:true},
        {header:'结束时间',dataIndex:'card_time_end',sort:true},
        {header:'卡券数据',dataIndex:'card_time_gift1_rate',width:350,renderer:function(val,m,r) {
            var html = ['<tr>'];
            Ext.each(attr.cardlist,function(item,i) {
                if(i >= 2) return;
                var id = item[0];
                html.push('<td>'+item[1]['CardName']+'</td>');
                html.push('<td>'+r.json['card_time_gift'+id+'_rate']*100+'%</td>');
                html.push('<td>'+r.json['card_time_gift'+id+'_count']+'</td>');
                html.push('<td>'+(r.json['card_time_gift'+id+'_count'] - r.json['card_time_gift'+id+'_count_drawn'])+'</td>');
                html.push('</tr><tr>');
               // html.push('<p>'+item[1]+'(概率:'+r.json['card_time_gift'+id+'_rate']+'&nbsp;&nbsp;数量:'+r.json['card_time_gift'+id+'_count']+'&nbsp;&nbsp;剩余:<b style="color:red;">'+(r.json['card_time_gift'+id+'_count'] - r.json['card_time_gift'+id+'_count_drawn'])+'</b>)</p>');
            });
            html.push('</tr>')
            return '<table><tr><th>列表</th><th>概率</th><th>总数</th><th>剩余数</th></tr>'+html.join('')+'</table>';
        }},
        {header:'创建时间',dataIndex:'card_time_time_create',width:140},
        {header:'操作',renderer:function(v,p,r){
            var str = '';
            str += Ext.rd('编辑','fireEdit',r,attr.ctrlName+'/editCardTime');
            str += Ext.rd('删除','fireDelete',r,attr.ctrlName+'/deleteCardTime','red');
            return str;
        }}
    ]
});
/*
| -------------------------------------------------------------------
|  List Grid Action
| -------------------------------------------------------------------
*/
list.grid.fireEdit = function(r) {
    var r = r.json;
    var win = new ZM.PopUp({
        title:'添加时间设置',
        defaults:{labelWidth: 120},
        items:{
            xtype:'formpanel',
            url:list.baseUrl+ attr.ctrlName+'/editCardTime',
            success:function(res) {
                ZM.bo.msg('success',res.detail);
                list.grid.store.reload();
                win.close();
            },
            params:{card_time_id:r.card_time_id},
            items:[{
                xtype:'displayfield',
                fieldLabel:'日期',
                format:'Y-m-d',
                value:r.card_time_date,
                name:'card_time_date_display',
                allowBlank:false
            },{
                xtype:'hidden',
                fieldLabel:'日期',
                value:r.card_time_date,
                name:'card_time_date',
                allowBlank:false
            },{
                xtype:'hidden',
                fieldLabel:'开始时间',
                value:'00:00:00',
                name:'card_time_start',
                value:r.card_time_start,
                format:'H:i:s',
                allowBlank:false
            },{
                xtype:'hidden',
                fieldLabel:'结束时间',
                value:'23:59:59',
                name:'card_time_end',
                value:r.card_time_end,
                format:'H:i:s',
                allowBlank:false
            }].concat((function() {
            	var arr = [];
            	Ext.each(attr.cardlist,function(item,i) {
                    if(i >= 2) return;
					arr.push({
						xtype:'numberfield',
                        fieldLabel:item[1]['CardName'] + '概率',
                        name:'card_time_gift'+item[0]+'_rate',
                        value:r['card_time_gift'+item[0]+'_rate'],
                        minValue:0,
                        maxValue:1,
                        allowBlank:false
					});
					arr.push({
						xtype:'intfield',
                        fieldLabel:item[1]['CardName'] + '数量',
                        minValue:0,
                        value:r['card_time_gift'+item[0]+'_count'],
                        name:'card_time_gift'+item[0]+'_count',
                        allowBlank:false
					});
            	});
            	return arr;
            })())
        }
    }).show();
}

list.grid.fireDelete = function(r) {
    var r = r.json;
    Ext.Msg.confirm('确定?','确定要删除时间段['+r.card_time_date+'('+r.card_time_start+'~'+r.card_time_end+')]?',function(btn) {
        if(btn == 'yes') {
            ZM.ajax({
                url: list.baseUrl + attr.ctrlName+'/deleteCardTime',
                params:{card_time_id:r.card_time_id},
                mask:list.grid,
                success:function(res){
                    ZM.bo.msg('success',res.detail);
                    list.grid.store.reload();
                }
            });
        }
    });
}


<?php include dirname(__FILE__).'/common/layout.php'; ?>
list.grid.store.load();
<?php include dirname(__FILE__).'/common/footer.php'; ?>